package io.renren.modules.contract.service.impl;

import io.renren.modules.project.dao.ProjectApplicantInfoDao;
import io.renren.modules.project.entity.ProjectApplicantInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.renren.modules.contract.dao.ContractPaymentDao;
import io.renren.modules.contract.entity.ContractPaymentEntity;
import io.renren.modules.contract.service.ContractPaymentService;



@Service("contractPaymentService")
public class ContractPaymentServiceImpl implements ContractPaymentService {
	@Autowired
	private ContractPaymentDao contractPaymentDao;
	@Autowired
	private ProjectApplicantInfoDao projectApplicantInfoDao;
	
	@Override
	public ContractPaymentEntity queryObject(String id){
		return contractPaymentDao.queryObject(id);
	}
	
	@Override
	public List<ContractPaymentEntity> queryList(Map<String, Object> map){
		return contractPaymentDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return contractPaymentDao.queryTotal(map);
	}
	
	@Override
	public void save(ContractPaymentEntity contractPayment){
		if (StringUtils.isNotBlank(contractPayment.getReportingUnit())) {
			String unitId = contractPayment.getReportingUnit();
			//判断输入的项目申请方是否存在
			ProjectApplicantInfoEntity unitCount = projectApplicantInfoDao.queryObject(contractPayment.getReportingUnit());
			//如果不存在则进行新增操作
			if (unitCount == null) {
				unitId = UUID.randomUUID().toString().replaceAll("-", "");
				ProjectApplicantInfoEntity projectApplicantInfoEntity = new ProjectApplicantInfoEntity();
				projectApplicantInfoEntity.setId(unitId);
				projectApplicantInfoEntity.setApplicantName(contractPayment.getReportingUnit());
				projectApplicantInfoDao.save(projectApplicantInfoEntity);
				contractPayment.setReportingUnit(unitId);
			}
		}
		contractPaymentDao.save(contractPayment);
	}
	
	@Override
	public void update(ContractPaymentEntity contractPayment){
		if (StringUtils.isNotBlank(contractPayment.getReportingUnit())) {
			String unitId = contractPayment.getReportingUnit();
			//判断输入的项目申请方是否存在
			ProjectApplicantInfoEntity unitCount = projectApplicantInfoDao.queryObject(contractPayment.getReportingUnit());
			//如果不存在则进行新增操作
			if (unitCount == null) {
				unitId = UUID.randomUUID().toString().replaceAll("-", "");
				ProjectApplicantInfoEntity projectApplicantInfoEntity = new ProjectApplicantInfoEntity();
				projectApplicantInfoEntity.setId(unitId);
				projectApplicantInfoEntity.setApplicantName(contractPayment.getReportingUnit());
				projectApplicantInfoDao.save(projectApplicantInfoEntity);
				contractPayment.setReportingUnit(unitId);
			}
		}
		contractPaymentDao.update(contractPayment);
	}
	
	@Override
	public void delete(String id){
		contractPaymentDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		contractPaymentDao.deleteBatch(ids);
	}
	
}
